import Utils from "../utils/Utils";

const { prefixName, assign, defaultTo } = Utils

/**
 * todo 调整为多边形 ： <br/> 支持 "边数" 和 "半径" （边到中心点距离）的参数
 */
class UTriangle implements UComponent{

    get name(): string {
        return this.options.name
    }

    get zIndex(): number {
        return this.options.zIndex
    }

    options: Required<UComponent.TriangleOpt> = {
        x: 0,
        y: 0,
        zIndex: 1,
        name: prefixName('u_triangle'),
        width: 50,
        height: 50,
        backgroundColor: 'transparent'
    }

    visible: boolean = true

    constructor(uTriangleOption?: UComponent.TriangleOpt) {
        this.setOption(uTriangleOption)
    }

    /**
    * 设置图形参数
    * @param uTriangleOption
    */
    setOption(uTriangleOption?: UComponent.RectOpt) {
        this.options = assign(this.options, defaultTo(uTriangleOption, {})) as Required<UComponent.TriangleOpt>
    }

    draw(ctx: CanvasRenderingContext2D) {
        const { x, y, height, backgroundColor } = this.options
        ctx.save()
        ctx.beginPath()
        ctx.moveTo(x, y + height / 2)
        ctx.lineTo(x + height / Math.sqrt(3), y - height / 2)
        ctx.lineTo(x + height / Math.sqrt(3) * 2, y + height / 2)
        ctx.closePath()
        ctx.fillStyle = backgroundColor
        ctx.fill()
    }
}

export default UTriangle